//
//  AVRequest.m
//  项目三
//
//  Created by rimi1 on 2016/12/7.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AVRequest.h"

@implementation AVRequest

// 查询
+ (void)avQueryWithQuery:(AVQuery *)query queryArr:(QueryArr)queryArr {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    [query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        if (queryArr) {
            // 返回数组，当数组为0时，表示不存在
            queryArr(objects, error);
        }
    }];
}
// 取出
+ (void)avFetchWithObj:(AVObject *)obj fetch:(AVFetch)fetch {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    [obj fetchInBackgroundWithBlock:^(AVObject * _Nullable object, NSError * _Nullable error) {
        if (fetch) {
            fetch(object, error);
        }
    }];
}
// 保存
+ (void)avSaveWithObj:(AVObject *)obj succeed:(AVSucceed)succeed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        if (succeed) {
            succeed(succeeded, error);
        }
        
    }];
}
// 添加relation
+ (void)avSaveAllAddWithObj:(AVObject *)obj key:(NSString *)key objArr:(NSArray *)objArr aSucceed:(AVSucceed)aSucceed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    AVRelation *relation = [obj relationForKey:key];
    
    [AVObject saveAllInBackground:objArr block:^(BOOL succeeded, NSError * _Nullable error) {
        for (AVObject *rObj in objArr) {
            [relation addObject:rObj];
        }
        [AVRequest avSaveWithObj:obj succeed:^(BOOL succeed, NSError *error) {
            aSucceed(succeed, error);
        }];
    }];
}
// 删除relation
+ (void)avSaveAllRemoveWithObj:(AVObject *)obj key:(NSString *)key objArr:(NSArray *)objArr rSucceed:(AVSucceed)rSucceed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    AVRelation *relation = [obj relationForKey:key];
    
    [AVObject saveAllInBackground:objArr block:^(BOOL succeeded, NSError * _Nullable error) {
        for (AVObject *rObj in objArr) {
            [relation removeObject:rObj];
        }
        [AVRequest avSaveWithObj:obj succeed:^(BOOL succeed, NSError *error) {
            rSucceed(succeed, error);
        }];
    }];
}
// 图片
+ (void)avImageWithData:(NSData *)data succeed:(AVImage)succeed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    UIApplication *app = [UIApplication sharedApplication];
    [app showCircleAnimationWithTitle:@"加载中"];
    
    NSDate *date = [NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyyMMddHHMMss";
    NSString *name = [NSString stringWithFormat:@"%@.png", [format stringFromDate:date]];
    
    AVFile *file = [AVFile fileWithName:name data:data];
    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        [app hideCircleAnimation];
        if (succeed) {
            succeed(file.url, succeeded, error);
        }
    } progressBlock:^(NSInteger percentDone) {
        
    }];
    
}
// 删除关系
+ (void)avRemoveRelationWithObj:(AVObject *)obj key:(NSString *)key succeed:(AVSucceed)rSucceed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [user relationForKey:key];
    
    [AVObject saveAllInBackground:@[obj] block:^(BOOL succeeded, NSError * _Nullable error) {
        if (error) {
            rSucceed(succeeded, error);
            return;
        }
        [relation removeObject:obj];
        
        [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
            if (rSucceed) {
                rSucceed(succeed, error);
            }
        }];
    }];
}
// 添加关系
+ (void)avAddRelationWithObj:(AVObject *)obj key:(NSString *)key succeed:(AVSucceed)aSucceed {
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
        [[RequestError sharedError] showErrorWithTitle:@"您的网络不给力啊，请检查网络"];
    }
    
    AVObject *user = [AVObject objectWithClassName:@"MyUser" objectId:[MyUser sharedUser].userId];
    AVRelation *relation = [user relationForKey:key];
    
    [AVObject saveAllInBackground:@[obj] block:^(BOOL succeeded, NSError * _Nullable error) {
        if (error) {
            aSucceed(succeeded, error);
            return;
        }
        [relation addObject:obj];
        
        [AVRequest avSaveWithObj:user succeed:^(BOOL succeed, NSError *error) {
            if (aSucceed) {
                aSucceed(succeeded, error);
            }
        }];
    }];
}




@end
